<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：登录控制器。
 *
 **/
namespace Api\Controller;

class LoginController extends BaseController
{
    
    public function _initialize(){
         //header('Content-Type:application/json; charset=utf-8');
//         if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
//             header('Access-Control-Allow-Origin:*');
//             header('Access-Control-Allow-Methods:GET, POST, PUT,DELETE,OPTIONS,PATCH');
//             header('Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Authorization,user-agent,token');
//             exit;
//         }
    }
    
    /**
     * 登录
     * @param number $type
     */
    public function index(){
        if(!IS_POST){
            $this->res['code'] = 100;
            $this->res['msg'] = '请用post方法请求接口';
            $this->response($this->res,'json');
        }
        $post_data = file_get_contents('php://input');
        
        $json = json_decode($post_data);
        
        $username = $json->mobile;
        $password = $json->password;
        
        if(!$username || !$password){
            $this->res['code'] = 101;
            $this->res['msg'] = '账户或密码为空';
            $this->response($this->res,'json');
        }
        $user = M('user')->where(array('username'=>$username))->find();
        if(empty($user)){
            $this->res['code'] = 102;
            $this->res['msg'] = '账户不存在请先注册';
            $this->response($this->res,'json');
        }
        if($user['password'] != md5($password)){
            $this->res['code'] = 103;
            $this->res['msg'] = '密码错误';
            $this->response($this->res,'json');
        }
        if($user['status'] != 1){
            $this->res['code'] = 104;
            $this->res['msg'] = '该账户已被禁用，请联系系统管理员';
            $this->response($this->res,'json');
        }
        M('user')->where('id='.$user['id'])->data(array('login_t'=>time()))->save();
        //更新用户token
        $num = M('user_token')->where('uid='.$user['id'])->count();
        $token = md5($username.'DZ'.time());
        $expire_t = time() + 60 * 60 * 24 * 7;
        if($num > 0){
            M('user_token')->data(array('token'=>$token,'expire_t'=>$expire_t,'update_t'=>time()))->where('uid='.$user['id'])->save();
        }else{
            M('user_token')->data(array('uid'=>$user['id'],'token'=>$token,'expire_t'=>$expire_t,'update_t'=>time()))->add();
        }
        $this->res['code'] = 200;
        $this->res['msg'] = '登录成功';
        $this->res['data']['token'] = $token;
        $this->res['data']['expire_t'] = date('Y-m-d H:i',$expire_t);
        $this->response($this->res,'json');
    }
    
    //验证手机号是否正确
    private function isphone($phone){
        if (!is_numeric($phone)) {
            return false;
        }
        return preg_match("/^1[34578]{1}\d{9}$/", $phone) ? true : false;
    }
    
    
    //获取随机数字字符串
    private function getRandomCode($length=4) {
        $glbVerifySeed = "1234567890";
        
        $bgnIdx = 0;
        $endIdx = strlen($glbVerifySeed)-1;
        
        $code = "";
        for($i=0; $i<$length; $i++) {
            $curPos = rand($bgnIdx, $endIdx);
            $code .= substr($glbVerifySeed, $curPos, 1);
        }
        
        return $code;
    }
    
    
}